Skip to content

Read the version of a build's formatter once in IntelliJ IDEA, and close its jars - #264

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/intellij-plugin-every-format-opens-the-builds-fo
Nov 23, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/intellij-plugin-every-format-opens-the-builds-fo

Conversation

@abashev

@abashev abashev commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #258. Based on 21.X.X; no other open pull request touches these files.

FormatterProvider.get asks whether the build's formatter is older than the plugin for every Reformat and every Optimize imports, before it asks its cache. In a project whose .idea/open-java-format.xml has an implementationClassPath, computeFormatterVersion answered by opening the class path's jars with new JarFile until it found the one with the formatter's service, and it closed none of them. Since #259 formats inside the IDE's JVM, a call takes milliseconds, and opening the jars for each one shows.

  • The jars are closed. FormatterVersions reads the manifest with the jar in try-with-resources.
  • The version is read once per set of jars. It is kept for a class path for as long as each jar keeps its size and modification time, as InProcessFormatters keeps a loaded formatter, for up to four class paths. A jar rebuilt in place is read again. The Jar record that keys both moves out of InProcessFormatters into a file of its own.
  • The URIs become paths with Path.of, as FormatterProvider makes them for formatting, rather than with URI.getPath.

A project without an implementationClassPath, which formats with the formatter bundled with the plugin, opened no jar before and opens none now.

What changes for users

  • Reformat and Optimize imports no longer open the build's formatter jars for every call, and the IDE holds none open between garbage collections. On Windows, a Gradle build or a cache cleanup no longer fails on a jar the IDE holds.
  • A formatter jar whose manifest has no Implementation-Version failed every format with a NullPointerException. It now has no version, as one whose version does not parse, and the plugin formats with the formatter bundled with it.
  • A class path in which no jar provides the formatter still fails, with an IllegalStateException that names the class path, where it was a RuntimeException saying "Couldn't find implementation JAR". Failures are not kept, so such a class path is still read for every call, with its jars closed.

Tests

FormatterVersionsTest writes the jars it reads:

  • the version is that of the jar that provides the formatter, not of a dependency before it;
  • a jar overwritten with zeros of its size and time still gives the version read before, so it was not opened again, and one rebuilt in place gives its new one;
  • a formatter jar without a version gives none, and a class path without the formatter says so;
  • on Linux, /proc/self/fd holds none of the jars once they are read. Only Linux lists a process's open files as paths in the file system, so the test runs on Linux only, which is where CI builds the plugin. I ran the class in an eclipse-temurin:21-jdk container: all five pass, and this one fails with the jar opened outside try-with-resources.

With the change, JDK 21: ./gradlew :open-java-format-idea-plugin:check passes, 134 tests.

Not tried in a running IDE.

FormatterProvider asks whether the build's formatter is older than
the plugin for every Reformat and every Optimize imports, before it
asks its cache. computeFormatterVersion answered by opening the jars
of the implementation class path with new JarFile until it found the
one with the formatter's service, and it closed none of them: each
stayed open until the garbage collector got to it, and on Windows a
jar the IDE holds open is one that a Gradle build cannot replace or
delete. Inside the IDE's JVM a call takes milliseconds, so opening
the jars for each one shows.

FormatterVersions reads the version with the jar in try-with-
resources, and keeps it for a class path for as long as its jars keep
their size and time, as InProcessFormatters keeps a formatter: a jar
rebuilt in place is read again, and the jars are not opened for every
file. The Jar record that keys both moves out of InProcessFormatters.
The class path's URIs become paths with Path.of, as FormatterProvider
makes them for formatting, rather than with URI.getPath.

A formatter jar whose manifest states no Implementation-Version
failed every format with a NullPointerException. It has no version
now, as one whose version does not parse, and the plugin formats with
the formatter bundled with it. A class path without the formatter
still fails, with the class path in the message.

FormatterVersionsTest writes the jars it reads. A jar overwritten
with zeros of its size and time still gives the version read before,
and one rebuilt in place gives its new one. On Linux the test looks
in /proc/self/fd for the jars once they are read, and fails when the
JarFile is not closed.

Fixes #258.
@abashev
abashev merged commit b366c3e into 21.X.X Nov 23, 2026
16 checks passed
@abashev
abashev deleted the abashev/intellij-plugin-every-format-opens-the-builds-fo branch November 23, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IntelliJ plugin: every format opens the build's formatter jars to read the version, and never closes them

1 participant